package com.ty.controller.admin;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ty.entity.SysMenus;
import com.ty.entity.SysRole;
import com.ty.service.MenusService;
import com.ty.util.Pager;

@Controller
@RequestMapping("/admin/menus")
public class MenusController {

	@Autowired
	private MenusService menusService;

	@RequestMapping("/index")
	public String index() {
		return "admin/menus/index";
	}

	// 2.列表页
	@RequestMapping("/indexList")
	public String indexList(int pageNum, int pageSize, SysMenus menus, ModelMap model) {
		Pager pager = menusService.selectByPager(pageNum, pageSize, menus);
		if (menus.getSysMenus().getId() > 0) {
			SysMenus rootMenus = menusService.getById(menus.getSysMenus().getId());
			model.put("rootMenus", rootMenus);
		}

		model.put("pager", pager);
		return "admin/menus/indexList";
	}

	// 5.保存或更新
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Map<String, Object> saveOrUpdate(SysMenus menus, int isUpdate) {
		boolean flag = false;
		if (isUpdate == 1) { // 更新操作
			if(menus.getSysMenus().getId().intValue()==0) {
				menus.setSysMenus(null);
			}
			flag = menusService.updateByObj(menus);
		} else { // 保存操作
			flag = menusService.saveObject(menus);
		}
		Map<String, Object> returnJson = new HashMap<>();
		if (flag) {
			returnJson.put("status", "1"); // 操作成功
			returnJson.put("msg", "操作成功！");
		} else {
			returnJson.put("status", "0"); // 操作失败
			returnJson.put("msg", "操作失败！");
		}
		return returnJson;

	}

	// 6.跳转到更新或保存页面
	@RequestMapping("/get")
	public String get(Long id, ModelMap model) {
		if (id != null) { // 调整到更新
			SysMenus menus = menusService.getById(id);
			String optionHtml= menusService.getMenus(menus);
			model.put("optionHtml", optionHtml);
			model.put("m", menus);
			model.put("isUpdate", 1); // 标记当前页面是更新
		} else {
		 	String optionHtml= menusService.getMenus(null);
		 	model.put("optionHtml", optionHtml);
			model.put("isUpdate", 0);// 标记当前页面是保存
		}

		return "admin/menus/saveOrUpdate";
	}

	// 3.单个删除
	@RequestMapping("/deleteOne")
	@ResponseBody
	public Map<String, Object> deleteOne(Long id) {
		boolean flag = menusService.deleteById(id);
		Map<String, Object> returnJson = new HashMap<>();
		if (flag) {
			returnJson.put("status", 1);
			returnJson.put("msg", "操作成功！");
		} else {
			returnJson.put("status", 0);
			returnJson.put("msg", "操作失败！");
		}
		return returnJson;
	}

	// 4.多个删除
	@RequestMapping("/batchDelete")
	@ResponseBody
	public Map<String, Object> batchDelete(String ids) {
		boolean flag = menusService.deleteBatch(ids);
		Map<String, Object> returnJson = new HashMap<>();
		if (flag) {
			returnJson.put("status", 1);
			returnJson.put("msg", "操作成功！");
		} else {
			returnJson.put("status", 0);
			returnJson.put("msg", "操作失败！");
		}
		return returnJson;
	}
}
